package com.notepad.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.notepad.R;
import com.notepad.domain.UserSetting;
import com.notepad.service.LockService;
import com.notepad.util.Constant;
import com.notepad.util.FileUtil;


/**
 * Created by jiamengyu on 1/19/2015.
 */
public class LockActivity extends Activity {
    private boolean mFinish = false;
    private final static String TAG = "LockActivity";
    private Button mButton;
    private EditText passwordEditText;
    private FileUtil mFileUtil;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lock);
        init();
        setListener();
    }

    private void setListener() {
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = passwordEditText.getText().toString();
                Log.d(TAG, "password" + password);
                if(password == null || "".equals(password)){
                    Toast.makeText(LockActivity.this,"密码不能为空，请输入密码！",Toast.LENGTH_SHORT).show();
                }
                if(password.equals(mFileUtil.getUserSetting().getPassword())){
                    mFinish = true;
                    finish();
                    //stopService(new Intent(LockActivity.this, LockService.class));
                }else{
                    Toast.makeText(LockActivity.this,"密码错误，请重新尝试",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void init() {
        passwordEditText = (EditText) findViewById(R.id.passwordEditText);
        mButton = (Button) findViewById(R.id.lockButton);
        mFileUtil = new FileUtil(LockActivity.this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        if(!mFinish){
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        Toast.makeText(this,"请您输入密码",Toast.LENGTH_SHORT).show();
    }
}
